package com.zhou.JavaAndroidFirm.presenter;

import com.zhou.JavaAndroidFirm.bean.NewsChannelTable;
import com.zhou.JavaAndroidFirm.constract.NewsChannelContract;
import com.zhou.JavaAndroidFirm.event.ChannelEvent;
import com.zhou.JavaAndroidFirm.rx.RxSubscriber;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2018/8/15 0015
 * @des
 */
public class NewsChanelPresenter extends NewsChannelContract.Presenter {


    @Override
    public void lodeChannelsRequest() {
        addSubscribe(mModel.lodeMineNewsChannels().subscribe(new RxSubscriber<List<NewsChannelTable>>(mContext) {
            @Override
            protected void _onNext(List<NewsChannelTable> newsChannelTables) {
                mView.returnMineNewsChannels(newsChannelTables);
            }

            @Override
            protected void _onError(String message) {

            }
        }));


        addSubscribe(mModel.lodeMoreNewsChannels().subscribe(new RxSubscriber<List<NewsChannelTable>>(mContext) {

            @Override
            protected void _onNext(List<NewsChannelTable> newsChannelTables) {
                mView.returnMoreNewsChannels(newsChannelTables);
            }

            @Override
            protected void _onError(String message) {

            }
        }));
    }

    @Override
    public void onItemSwap(final ArrayList<NewsChannelTable> newsChannelTableList, int fromPosition, int toPosition) {
        addSubscribe(mModel.swapDb(newsChannelTableList, fromPosition, toPosition).subscribe(new RxSubscriber<String>(mContext) {
            @Override
            protected void _onNext(String s) {
                EventBus.getDefault().post(new ChannelEvent().setTables(newsChannelTableList));
            }

            @Override
            protected void _onError(String message) {

            }
        }));
    }

    @Override
    public void onItemAddOrRemove(final ArrayList<NewsChannelTable> mineChannelTableList, ArrayList<NewsChannelTable> moreChannelTableList) {
        addSubscribe(mModel.updateDb(mineChannelTableList, moreChannelTableList).subscribe(new RxSubscriber<String>(mContext) {
            @Override
            protected void _onNext(String s) {
                EventBus.getDefault().post(new ChannelEvent().setTables(mineChannelTableList));
            }

            @Override
            protected void _onError(String message) {

            }
        }));
    }
}
